package org.example;

import org.example.bean.Token;
import org.example.bean.TokenType;

import java.util.ArrayList;
import java.util.List;

public class TokensAnalyzer {
    int st=0;
    int end=0;
    char[] program;

    public List<Token> getTokens(char[] str){
        //判断str[st]是否是标识符
        List<Token> tokens =new ArrayList<>();
        program=str;
        while (true)
        {
            Token token = nextToken();
            tokens.add(token);
            if (token.type == TokenType.END){
//                System.out.println(token.type.name()+" $");
                break;
            }
//            System.out.println(token.type.name()+" "+token.value);
        }
        return tokens;
    }

    Token nextToken() {

        Token cur_token = new Token();
        int len=program.length;
        if(end==len){
            cur_token.type=TokenType.END;
            cur_token.value="$";
            return cur_token;
        }
        if (program[end] == ' ' || program[end] == '\t' || program[end] == '\n') {
            st++;
            end++;
            return nextToken();
        } else if (Character.isLetter(program[end]) || program[end] == '_') {
            end++;
            while (Character.isLetter(program[end]) || Character.isDigit(program[end]) || program[end] == '_')
                end++;
            end--;
            char[] str = new char[end - st + 1];
            System.arraycopy(program, st, str, 0, end - st + 1);
            if (isKeyWord(cur_token, str)) {
                // Handle keyword case
            } else {
                if (!isID(cur_token, str)) {
                    cur_token.type = TokenType.ERROR;
                    cur_token.value = new String(str);
                }
            }
        } else if (Character.isDigit(program[end])) {
            end++;
            while (Character.isDigit(program[end]))
                end++;
            if (Character.isLetter(program[end]) || program[end] == '_') {
                while (Character.isLetter(program[end]) || Character.isDigit(program[end]) || program[end] == '_') {
                    end++;
                }
                char[] str = new char[end - st];
                System.arraycopy(program, st, str, 0, end - st);
                System.out.println(new String(str) + "不是标识符");
                cur_token.value = new String(program, st, end - st);
                st = end;
                cur_token.type = TokenType.ERROR;
                return cur_token;
            }
            end--;
            cur_token.type = TokenType.NUM;
        } else if (program[end] == '<') {
            end++;
            if (program[end] == '=') {
                cur_token.type = TokenType.LE;
                return cur_token;
            } else if (program[end] == '>') {
                cur_token.type = TokenType.NE;
                return cur_token;
            } else {
                end--;
                cur_token.type = TokenType.LT;
            }
        } else if (program[end] == ',') {
            cur_token.type = TokenType.COMMA;
        } else if (program[end] == '(') {
            cur_token.type = TokenType.OPEN_P;
        } else if (program[end] == ')') {
            cur_token.type = TokenType.CLOSE_P;
        } else if (program[end] == '{') {
            cur_token.type = TokenType.OPEN_BRACE;
        } else if (program[end] == '}') {
            cur_token.type = TokenType.CLOSE_BRACE;
        } else if (program[end] == ';') {
            cur_token.type = TokenType.SEMI;
        } else if (program[end] == '=') {
            if (program[end + 1] != '=')
                cur_token.type = TokenType.ASSIGN;
            else if (program[end + 1] == '=')
                cur_token.type = TokenType.EQ;
        } else if (program[end] == '\0') {
            cur_token.type = TokenType.END;
        } else if (program[end] == '+') {
            cur_token.type = TokenType.ADD;
        } else if (program[end] == '-') {
            cur_token.type = TokenType.SUB;
        } else if (program[end] == '*') {
            cur_token.type = TokenType.MUL;
        } else if (program[end] == '/') {
            cur_token.type = TokenType.DIV;
        }
        while (program[st] == ' ') {
            st++;
        }
        cur_token.value = new String(program, st, end - st + 1);
        st = end + 1;
        end = st;
        return cur_token;
    }

        boolean isID(Token token, char[] str) {
            int index = 0;
                if (Character.isLetter(str[0])) {
                    while (Character.isLetter(str[index]) || Character.isDigit(str[index]) || str[index] == '_') {
                        index++;
                    }
                    if (str[index] == ';' || str[index] == ',' || str[index] == '(' || str[index] == ')' || str[index] == ' ' || str[index] == '\0') {
                        token.type = TokenType.ID;
                        token.value = new String(str);
                        return true;
                    } else {
                        System.out.println("异常符号");
                    }
                }
            return false;
    }

    private boolean isKeyWord(Token curToken, char[] str) {
        String tmp=new String(str);
        if (tmp.equals("main")) {
            curToken.type = TokenType.ID;
            curToken.value = new String(str);
        }
        curToken.type = TokenType.ID;
        curToken.value = new String(str);
        return true;
    }
    public void test(){
        char[] str= "main".toCharArray();
        String tmp=new String(str);

        System.out.println(tmp);
        String s="main";
        System.out.println(s);
        if(tmp.equals(s)){
            System.out.println("s==tmp");
        }
    }

}
